$OpenBSD: patch-dbus_dbus-sysdeps-util-unix_c,v 1.15 2015/08/27 06:46:46 ajacoutot Exp $

XXX push upstream

--- dbus/dbus-sysdeps-util-unix.c.orig	Fri Jan 30 13:46:24 2015
+++ dbus/dbus-sysdeps-util-unix.c	Wed Aug 26 10:50:04 2015
@@ -62,6 +62,10 @@
 #include <systemd/sd-daemon.h>
 #endif
 
+#ifdef __OpenBSD__
+#include <sys/sysctl.h>
+#endif
+
 #ifndef O_BINARY
 #define O_BINARY 0
 #endif
@@ -1213,7 +1217,13 @@ _dbus_command_for_pid (unsigned long  pid,
   /* This is all Linux-specific for now */
   DBusString path;
   DBusString cmdline;
+#ifndef __OpenBSD__
   int fd;
+#else
+  char **argv, **args;
+  int mib[4];
+  size_t len;
+#endif
   
   if (!_dbus_string_init (&path)) 
     {
@@ -1228,6 +1238,7 @@ _dbus_command_for_pid (unsigned long  pid,
       return FALSE;
     }
   
+#ifndef __OpenBSD__
   if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
     goto oom;
   
@@ -1255,6 +1266,37 @@ _dbus_command_for_pid (unsigned long  pid,
   
   if (!_dbus_close (fd, error))
     goto fail;
+#else /* OpenBSD */
+  args = NULL;
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_PROC_ARGS;
+  mib[2] = pid;
+  mib[3] = KERN_PROC_ARGV;
+
+  if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1)
+    goto fail;
+
+  args = malloc (len);
+  if (args == NULL)
+    goto fail;
+
+  if (sysctl (mib, 4, args, &len, NULL, 0) == -1)
+    goto fail;
+
+  for (argv = args; *argv != NULL; argv++) {
+    if (argv != args)
+      {
+        if (!_dbus_string_append_printf (&cmdline, " "))
+          goto fail;
+      }
+    if (!_dbus_string_append_printf (&cmdline, "%s", *argv))
+      goto fail;
+  }
+
+  if (_dbus_string_get_length (&cmdline) > max_len)
+    _dbus_string_set_length (&cmdline, max_len);
+
+#endif
   
   string_squash_nonprintable (&cmdline);  
 
@@ -1263,12 +1305,18 @@ _dbus_command_for_pid (unsigned long  pid,
 
   _dbus_string_free (&cmdline);  
   _dbus_string_free (&path);
+#ifdef __OpenBSD__
+  free (args);
+#endif
   return TRUE;
 oom:
   _DBUS_SET_OOM (error);
 fail:
   _dbus_string_free (&cmdline);
   _dbus_string_free (&path);
+#ifdef __OpenBSD__
+  free (args);
+#endif
   return FALSE;
 }
 
